package com.batdream.message.service.impl;

import com.batdream.message.mapper.MyFriendsMapper;
import com.batdream.message.mapper.UsersMapperCustom;
import com.batdream.message.pojo.MyFriends;
import com.batdream.message.pojo.Users;
import com.batdream.message.pojo.vo.MyFriendsVo;
import com.batdream.message.service.MyFriendsService;
import com.batdream.message.service.UserService;
import com.batdream.message.utils.JSONResult;
import com.batdream.message.utils.idworker.Sid;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Service
public class MyFriendsServiceImpl implements MyFriendsService {

    private final MyFriendsMapper myFriendsMapper;
    private final UsersMapperCustom usersMapperCustom;

    @Autowired
    public MyFriendsServiceImpl(MyFriendsMapper myFriendsMapper, UsersMapperCustom usersMapperCustom) {
        this.myFriendsMapper = myFriendsMapper;
        this.usersMapperCustom = usersMapperCustom;
    }

    @Override
    public MyFriends searchMyfriendByUsersId(MyFriends friends) {
        return myFriendsMapper.selectOne(friends);
    }

    @Override
    public Integer addFriend(String myUserId, String myFriendUserId) {
        MyFriends myFriends = new MyFriends();
        myFriends.setId(Sid.next());
        myFriends.setMyUserId(myUserId);
        myFriends.setMyFriendUserId(myFriendUserId);
        return myFriendsMapper.insert(myFriends);
    }

    @Override
    public JSONResult checkIsNotFriend(String myUserId, String friendId) {
        MyFriends myFriends = new MyFriends();
        myFriends.setMyUserId(myUserId);
        myFriends.setMyFriendUserId(friendId);
        myFriends = searchFriendByTwinsId(myFriends);

        if (Objects.isNull(myFriends)){
            return JSONResult.errorMsg("");
        }
        return JSONResult.ok();
    }

    @Override
    public List<MyFriendsVo> getMyFriends(String userId) {
        return usersMapperCustom.getMyFriends(userId);
    }

    /**
     * 根据双方id查询是否是朋友
     * @param myFriends
     * @return
     */
    private MyFriends searchFriendByTwinsId(MyFriends myFriends) {
        return myFriendsMapper.selectOne(myFriends);
    }
}
